package com.team.six.bluecatshop.controller;

import com.team.six.bluecatshop.domain.Activity;
import com.team.six.bluecatshop.domain.Product;
import com.team.six.bluecatshop.domain.SeachActivityReponse;
import com.team.six.bluecatshop.service.ActivityService;
import com.team.six.bluecatshop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@Controller
@RequestMapping("/adminActivity")
public class AdminActivityController {
    @Autowired
    ProductService productService;
    @Autowired
    ActivityService activityService;

    /**
     * 跳转至商品展示页面
     */
    @GetMapping("/activityList")
    public String activityGetList(){
        return "administration/activity_list";
    }

    /**
     * 处理商品展示请求
     */
    @GetMapping("/api/activityList")
    @ResponseBody
    public SeachActivityReponse seachActivityResponse(Integer pageNum, Integer pageSize, String keyword){

        return activityService.findActivityAll(pageNum,pageSize,keyword);
    }
    /**
     * 跳转至新增商品请求页面并展示商品类型
     */
    @GetMapping("/addActivityShow")
    public String addProductShow(Model model){
        List<Product> products = productService.findAll();
        model.addAttribute("products",products);
        return "administration/activity_add";
    }

    /**
     * 新增商品请求
     */
    @PostMapping("/addActivity")
    public String addActivity(Activity activity, Model model){
       // System.out.println(product.getPtypeId());
        System.out.println(activity);
        if (activity.getActivityId().equals("")){
         //   System.out.println(activity.getTimeEnd());
           if (activityService.findActivityByProductId(activity.getProductId())!=null){
               model.addAttribute("msg","exist");
               List<Product> products = productService.findAll();
               model.addAttribute("products",products);
               return "administration/activity_add";
           }else{
                activityService.add(activity);
            }
        }else {
            activityService.updateByPrimaryKey(activity);
        }

        return "redirect:/adminActivity/activityList";
    }

    /**
     * 处理删除请求
     */
    @GetMapping("/deleteActivity")
    public String deleteActivity(String activityId){
        activityService.remove(activityId);
        return "redirect:/adminActivity/activityList";
    }

    /**
     * 跳转至修改商品页面并展示商品类型
     */
    @GetMapping("/editActivityShow")
    public String editActivityShow(Activity activityEdit,Model model,String activityDescribe){
        System.out.println(activityEdit.getActivitytName());
        model.addAttribute("activityEdit",activityEdit);
        List<Product> products = productService.findAll();
        model.addAttribute("products",products);
        return "administration/activity_add";
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDelete")
    @ResponseBody
    public String batchDelete(String activities){
        String[] split = activities.split(",");
        for (int i=0;i<split.length;i++){
           // System.out.println(split[i]);
            activityService.remove(split[i]);
        }
     //   System.out.println(products);
        return "删除成功";
    }
}
